<template>
    <el-upload
            class="avatar-uploader"
            :action="uploadImgUrl"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload">
        <img v-if="imageUrl" :src="imageUrl" class="circleImg">
        <i v-else class="el-icon-plus avatar-uploader-icon"></i>
    </el-upload>
</template>

<script>
  import Vue from 'vue'
  export default {
    name: 'uploadCover',
    data () {
      return {
        imageUrl: '',
        uploadImgUrl: this.$http.adornUrl('/oss/oss/upload?token=') + Vue.cookie.get('token') + '&client=ADMIN'
      }
    },
    methods: {
      handleAvatarSuccess (response, file, fileList) {
        console.log(response)
        if (response && response.code === 200) {
          this.$router.push({
            name: 'member-add-or-update',
            query: {
              id: response.data.id
            }
          })
        }
        this.imageUrl = URL.createObjectURL(file.raw)
      },
      // handleAvatarSuccess (response, file, fileList) {
      //   console.log(response)
      //   this.dataForm.ossId = response.data.id
      // },
      beforeAvatarUpload (file) {
        const isJPG = file.type === 'image/jpeg'
        const isLt2M = file.size / 1024 / 1024 < 2

        if (!isJPG) {
          this.$message.error('上传头像图片只能是 JPG 格式!')
        }
        if (!isLt2M) {
          this.$message.error('上传头像图片大小不能超过 2MB!')
        }
        return isJPG && isLt2M
      }
    }
  }
</script>

<style scoped>
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }

  .avatar-uploader .el-upload:hover {
    border-color: #409eff;
  }
  .circleImg {
    width: 150px;
    height: 150px;
  }


  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 150px;
    height: 150px;
    line-height: 150px;
    text-align: center;
  }
</style>
